<template>
  <el-dialog title="新增员工" :visible="value" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="addEmployee" label-width="30%" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input placeholder="请输入姓名" style="width:70%" v-model="formData.username"/>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input placeholder="请输入手机号" style="width:70%" v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker placeholder="请输入入职时间"  style="width:70%" v-model="formData.timeOfEntry"/>
      </el-form-item>
      <el-form-item label="聘用形式"  prop="formOfEmployment">
        <el-select placeholder="请选择聘用形式" style="width:70%" v-model="formData.formOfEmployment" >
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input type="number" placeholder="请输入工号"  style="width:70%" v-model="formData.workNumber"/>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input placeholder="请选择部门" style="width:70%" v-model="formData.departmentName" @focus="getDepartments"/>
        <!-- 树形的部门数据 -->
        <el-tree v-if="showTree" v-loading="loading" :data="list" :props="defaultProps" :default-expand-all="true" @node-click="selectedDepartment"></el-tree>
      </el-form-item>
      <el-form-item label="转正日期" prop="correctionTime">
        <el-date-picker placeholder="请选择日期"  style="width:70%" v-model="formData.correctionTime"/>
      </el-form-item>
    </el-form>
    <!-- dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row
      slot="footer"
      type="flex"
      justify="center"
    >
      <el-col :span="6">
        <el-button
          size="small"
          @click="closeDialog"
        >取消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="btnOk"
        >确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
export default {
  // 组件名称
  name: 'AddEmployee',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {
    value: {
      type: Boolean,
      required: true
    }
  },
  // 组件状态值
  data () {
    return {
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      // 校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 2, max: 4, message: '用户姓名为2-4位', trigger: 'blur'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }, {
          pattern: /^\d+$/, message: '工号格式不正确', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }]
      },
      // 部门数据
      list: [],
      // 树形数据的设置
      defaultProps: {
        label: 'name'
      },
      showTree: false,
      loading: false, // 控制树的显示或者隐藏进度条
      EmployeeEnum // 内涵聘用形式的对照规则
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，el属性还不存在
  */
  created () {
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted () {},
  // 组件方法
  methods: {
    async getDepartments () {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.list = tranListToTreeData(depts, '')
      this.loading = false
    },
    selectedDepartment (node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async btnOk () {
      // 校验
      await this.$refs.addEmployee.validate()
      // 调新增接口
      await addEmployee(this.formData)
      // 调用父组件的方法
      this.$parent.getEmployeeList()
      this.$emit('input', false)
    },
    closeDialog () {
      this.$refs.addEmployee.resetFields()
      this.$emit('input', false)
    }
  }
}
</script>

<style scoped lang="less">

</style>
